package Test03;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class InventoryManagementSystem {
//    	使用java.util.List和java.util.Map来存储和管理商品信息。
    static private Map<String, Product> productMap = new HashMap<>();
//    添加商品、删除商品、更新库存、检查过期商品
    public void addProduct(Product  product) {
        productMap.put(product.getId(), product);
    }
    public void removeProduct(String id) {
        productMap.remove(id);
    }
    public void updateStock(String id, int stock) {
        Product product = productMap.get(id);
        if (product != null) {
            product.setStock(stock);
        }
    }
    public void checkExpiredProducts() {
        for (Product product : productMap.values()) {
            if (product.getExpirationDate().isBefore(LocalDate.now())) {
                System.out.println("商品" + product.getName() + "已过期");
            }
        }
    }
    public void showAllProducts() {
        //    查看所有商品信息
        for (Product product : productMap.values()) {
            System.out.println(product);
        }
    }
    public static void main(String[] args) {
        InventoryManagementSystem inventoryManagementSystem = new InventoryManagementSystem();
        inventoryManagementSystem.addProduct(new Product("a", "a", 10, 10, LocalDate.of(2023, 1, 1)));
        inventoryManagementSystem.addProduct(new Product("b", "b", 20, 20, LocalDate.of(2023, 2, 1)));
        inventoryManagementSystem.addProduct(new Product("c", "c", 30, 30, LocalDate.of(2023, 3, 1)));
//        通过Random生成随机的商品属性，如价格、库存数量等
        for (int i = 0; i < 10; i++) {
            String id = "p" + i;
            String name = "商品" + i;
            double price = Math.random() * 100;
//            price 保留两位小数
            price = Math.round(price * 100) / 100.0;
            int stock = (int) (Math.random() * 100);
            LocalDate expirationDate = LocalDate.now().plusDays((long) (Math.random() * 365));
            inventoryManagementSystem.addProduct(new Product(id, name, price, stock, expirationDate));
        }
        while (true) {
            System.out.println("----------------------库存管理系统----------------------");
            System.out.println("1.添加商品");
            System.out.println("2.删除商品");
            System.out.println("3.更新库存");
            System.out.println("4.检查过期商品");
            System.out.println("5.查看所有商品");
            System.out.println("6.退出");
            System.out.print("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            String id,name;
            double price;
            int stock;
            LocalDate expirationDate;
            switch (choice) {
                case 1:
                    while (true) {
                        System.out.print("请输入商品ID：");
                        id = sc.next();
                        if (!productMap.containsKey(id)) break;
                        System.out.println("商品ID已存在，请重新输入");
                    }
                    while (true){
                        System.out.println("请输入商品名称：");
                        name = sc.next();
                        if (!name.isEmpty()) break;
                    }
                    while (true) {
                        System.out.print("请输入商品价格：");
                        price = sc.nextDouble();
                        if (price > 0) break;
                        System.out.println("价格必须大于0，请重新输入");
                    }
                    while (true) {
                        System.out.print("请输入商品库存数量：");
                        stock = sc.nextInt();
                        if (stock >= 0) break;
                        System.out.println("库存数量必须大于等于0，请重新输入");
                    }
                    while (true) {
                        System.out.print("请输入商品过期日期（格式：yyyy-MM-dd）：");
                        String dateStr = sc.next();
                        try {
                            expirationDate = LocalDate.parse(dateStr);
                            break;
                        } catch (Exception e) {
                            System.out.println("日期格式错误，请重新输入");
                        }
                    }
                    inventoryManagementSystem.addProduct(new Product(id, name, price, stock, expirationDate));
                    break;
                case 2:
                    while (true) {
                        System.out.print("请输入要删除的商品ID：");
                        id = sc.next();
                        if (productMap.containsKey(id)) break;
                        System.out.println("商品ID不存在，请重新输入");
                    }
                    inventoryManagementSystem.removeProduct(id);
                    System.out.println("商品" + id + "已删除");
                    break;
                case 3:
                    while (true) {
                        System.out.print("请输入要更新库存的商品ID：");
                        id = sc.next();
                        if (productMap.containsKey(id)) break;
                        System.out.println("商品ID不存在，请重新输入");
                    }
                    while (true) {
                        System.out.print("请输入新的库存数量：");
                        stock = sc.nextInt();
                        if (stock >= 0) break;
                        System.out.println("库存数量必须大于等于0，请重新输入");
                    }
                    inventoryManagementSystem.updateStock(id, stock);
                    break;
                case 4:
                    inventoryManagementSystem.checkExpiredProducts();
                    break;
                case 5:
                    inventoryManagementSystem.showAllProducts();
                    break;
                case 6:
                    System.out.println("------------------------再见------------------------");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
                    break;
                }
            }
        }
}


